﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;

namespace G.Core.Common
{
    public class UtilityXML
    {
        #region 获取XML内容
        /// <summary>
        /// 获取XML内容
        /// </summary>
        /// <param name="sXml"></param>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public static string GetXmlNodeText(string sXml, string xPath)
        {
            string sResult = "";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(sXml);
                sResult = doc.SelectSingleNode(xPath).InnerText.ToString();
            }
            catch (Exception)
            {
                sResult = "-999999";
            }
            return sResult;
        }
        #endregion

        #region 类转化为XML
        /// <summary>
        /// 类转化为XML
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string Class2XML(object o)
        {
            string rStr = "";
            foreach (System.Reflection.PropertyInfo info in o.GetType().GetProperties())
            {
                rStr = rStr + "<" + info.Name + ">" + info.GetValue(o, null) + "</" + info.Name + ">";
            }
            return rStr;
        }
        #endregion

        #region 将DataTable对象转换成XML字符串
        /// <summary>
        /// 将DataTable对象转换成XML字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string CDataToXml(DataTable dt)
        {
            if (dt != null)
            {
                MemoryStream ms = null;
                XmlTextWriter XmlWt = null;
                try
                {
                    ms = new MemoryStream();
                    //根据ms实例化XmlWt
                    XmlWt = new XmlTextWriter(ms, Encoding.Unicode);
                    //获取ds中的数据
                    dt.WriteXml(XmlWt);
                    int count = (int)ms.Length;
                    byte[] temp = new byte[count];
                    ms.Seek(0, SeekOrigin.Begin);
                    ms.Read(temp, 0, count);
                    //返回Unicode编码的文本
                    UnicodeEncoding ucode = new UnicodeEncoding();
                    string returnValue = ucode.GetString(temp).Trim();
                    return returnValue;
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    //释放资源
                    if (XmlWt != null)
                    {
                        XmlWt.Close();
                        ms.Close();
                        ms.Dispose();
                    }
                }
            }
            else
            {
                return "";
            }
        }
        #endregion

        #region 将DataSet对象中指定的Table转换成XML字符串
        /// <summary>
        /// 将DataSet对象中指定的Table转换成XML字符串
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="tableIndex"></param>
        /// <returns></returns>
        public static string CDataToXml(DataSet ds, int tableIndex)
        {
            if (tableIndex != -1)
            {
                return CDataToXml(ds.Tables[tableIndex]);
            }
            else
            {
                return CDataToXml(ds.Tables[0]);
            }
        }
        #endregion


        public static string CDataToXmlEx(DataTable dt, string marks = "")
        {
            StringBuilder strXML = null;
            try
            {
                strXML = new StringBuilder();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (marks != "")
                        strXML.Append("<" + marks + ">");
                    for (int j = 0; j < dt.Columns.Count; j++)
                        strXML.Append("<" + dt.Columns[j].ColumnName + ">" + dt.Rows[i][j] + "</" + dt.Columns[j].ColumnName + ">");
                    if (marks != "")
                        strXML.Append("</" + marks + ">");
                }
                return strXML.ToString();
            }
            catch (IOException)
            {
                return "";
            }
        }
    }
}
